package fire.and.near.challenger;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import fire.and.near.challenger.database.ChallengeModel;
import fire.and.near.challenger.database.DatabaseActivity;
import fire.and.near.challenger.database.DatabaseResponse;
import fire.and.near.challenger.database.TableConstants.ChallengesTable;
import fire.and.near.challenger.database.TableConstants.FriendsTable;
import fire.and.near.challenger.dialogs.ChallengeDialogFragment;
import fire.and.near.challenger.util.Consts;
import fire.and.near.challenger.web.ServerResponse;
import fire.and.near.challenger.web.WebActivity;

public class AcceptChallengeActivity extends ActionBarActivity implements
		DatabaseActivity, WebActivity {

	private ChallengeModel mChallenge = null;
	private boolean databaseWorking = false;

	private TextView textViewDescription = null;
	private TextView textViewFriendName = null;

	private AdView adView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_accept_challenge);

		adView = (AdView) this.findViewById(R.id.adView2);
		AdRequest adRequest = new AdRequest.Builder().addTestDevice(
				AdRequest.DEVICE_ID_EMULATOR).build();

		// Start loading the ad in the background.
		adView.loadAd(adRequest);

		if (savedInstanceState == null) {
			initChallenge(getIntent().getExtras());
		}

		this.textViewDescription = (TextView) findViewById(R.id.textView_accept_description);
		this.textViewFriendName = (TextView) findViewById(R.id.textView_accept_friend_name);

		this.textViewDescription.setText(this.mChallenge.getDescription());
		this.textViewFriendName.setText(this.mChallenge.getChallenger()
				.getName() != null ? this.mChallenge.getChallenger().getName()
				: Consts.COMPANY_NAME);
	}

	@Override
	protected void onPause() {

		if (adView != null) {
			adView.pause();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (adView != null) {
			adView.resume();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		if (adView != null) {
			adView.destroy();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putInt(ChallengesTable.ID, this.mChallenge.getID());
		outState.putString(ChallengesTable.DESCRIPTION,
				this.mChallenge.getDescription());
		outState.putString(ChallengesTable.HASHTAG,
				this.mChallenge.getHashtag());
		outState.putInt(ChallengesTable.CHALLENGE_TYPE,
				this.mChallenge.getChallengeTypeForDatabase());
		outState.putLong(ChallengesTable.CHALLENGER_ID, this.mChallenge
				.getChallenger().getFbID());
		outState.putString(FriendsTable.NAME, this.mChallenge.getChallenger()
				.getName());
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		initChallenge(savedInstanceState);
		super.onRestoreInstanceState(savedInstanceState);
	}

	private void initChallenge(Bundle extras) {
		this.mChallenge = new ChallengeModel();
		this.mChallenge.setID(extras.getInt(ChallengesTable.ID));
		this.mChallenge.setDescription(extras
				.getString(ChallengesTable.DESCRIPTION));
		this.mChallenge.setHashtag(extras.getString(ChallengesTable.HASHTAG));
		this.mChallenge.setChallengeType(extras
				.getInt(ChallengesTable.CHALLENGE_TYPE));
		this.mChallenge.getChallenger().setFbID(
				extras.getLong(ChallengesTable.CHALLENGER_ID));
		this.mChallenge.getChallenger().setName(
				extras.getString(FriendsTable.NAME));
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_logout) {
			LoginActivity.mFbHelper.logout();
			finish();
			return true;
		}

		if (id == R.id.action_details) {
			Intent i = new Intent(this, UserDetailsActivity.class);
			startActivity(i);

			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	public void acceptChallenge(View view) {

		if (databaseWorking) {
			return;
		}
		databaseWorking = true;

		Intent i = new Intent(this, CompleteChallengeActivity.class);

		Bundle extras = new Bundle();
		extras.putInt(ChallengesTable.ID, mChallenge.getID());
		extras.putString(ChallengesTable.DESCRIPTION,
				mChallenge.getDescription());
		extras.putString(ChallengesTable.HASHTAG, mChallenge.getHashtag());
		extras.putInt(ChallengesTable.CHALLENGE_TYPE,
				mChallenge.getChallengeTypeForDatabase());
		extras.putLong(ChallengesTable.CHALLENGER_ID, mChallenge
				.getChallenger().getFbID());
		extras.putBoolean(ChallengesTable.ACCEPTED, mChallenge.isAccepted());

		i.putExtras(extras);
		startActivity(i);

		finish();
	}

	public void declineChallenge(View view) {
		if (databaseWorking) {
			return;
		}
		databaseWorking = true;

		Bundle b = new Bundle();
		b.putInt(ChallengesTable.ID, this.mChallenge.getID());
		b.putString(Consts.QUESTION,
				getResources().getString(R.string.question_decline));
		b.putInt(Consts.OPTION, 1);

		ChallengeDialogFragment dialog = new ChallengeDialogFragment();
		dialog.setArguments(b);
		dialog.show(getSupportFragmentManager(),
				ChallengeDialogFragment.class.getName());
	}

	@Override
	public void processDatabaseResponse(DatabaseResponse result) {
		if (result.isSuccessful()) {
			Toast.makeText(this, Consts.CHALLENGE_DECLINED, Toast.LENGTH_LONG)
					.show();
		}
		finish(); // go back to main activity
	}

	@Override
	public void setDatabaseWorking(boolean working) {
		this.databaseWorking = working;
	}

	@Override
	public void processSeverResponse(ServerResponse response) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setHttpWorking(boolean working) {
		// TODO Auto-generated method stub

	}
}
